<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
namespace Qwadmin\Controller;

use Common\Controller\OdooApiController;

class GradeController extends ComController {
    
    public function index(){
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $pagesize = 20; #每页数量
        $count = M('grade')->count();
        $offset = $pagesize * ($p - 1); //计算记录偏移量
        $list = M('grade')->limit($offset . ',' . $pagesize) ->select();
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('lists',$list);
        $this->assign('page',$page);
        $this->display();
    }
    public function add(){
        $m = M('grade');
        if(IS_POST){
            $data = I('post.');
            $data['authority'] = serialize($data['authority']);
            $data['create_time'] = time();
            if($m->add($data)){
                $this->success('恭喜，等级添加成功！',U('index'));
            } else {
                $this->error('添加失败！');
            }
        }else{
            $this->display('form');
        }
    }
    public function edit(){
        $id = I('get.id');
         $m = M('grade');
        
        if(IS_POST){
            $data = I('post.');
            $data['authority'] = serialize($data['authority']);
            if($m->where(array('id'=>$id))->save($data)){
                $this->success('恭喜，等级修改成功！',U('index'));
            } else {
                $this->error('修改失败！');
            }
        }else{
            $info = $m->where(array('id'=>$id))->find();
           $info['authority'] = unserialize($info['authority']);
      
            $this->assign('info',$info);
            $this->display('form');
        }
    }
    public function del(){
      $ids = isset($_REQUEST['ids']) ? $_REQUEST['ids'] : false;
        //uid为1的禁止删除
        if (is_array($ids)) {
            foreach ($ids as $k => $v) {
                if($v == 1){
                    $this->error('id为1的不能删除！');
                }
                $ids[$k] = intval($v);
            }
        }
         if($ids == 1){
                    $this->error('id为1的不能删除！');
                }
        $map['id'] = array('in', $ids);
       
        if (M('grade')->where($map)->delete()) {
            addlog('删除等级ID：' . $ids);
            $this->success('恭喜，等级删除成功！');
        } else {
            $this->error('参数错误！');
        }  
        
    }
}
